package com.xiaoxie.cloud.controller;

import com.xiaoxie.cloud.entities.Pay;
import com.xiaoxie.cloud.entities.PayDTO;
import com.xiaoxie.cloud.resp.ResultData;
import com.xiaoxie.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequiredArgsConstructor
@Tag(name="支付微服务模块", description = "支付CRUD")
public class PayController {

    private final PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "新增", description = "新增支付信息,入参为json格式")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        log.info(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("新增成功，影响记录行数：" + i);
    }

    @DeleteMapping("/pay/del/{id}")
    @Operation(summary = "删除", description = "根据id删除支付信息")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        if (id<=0) {
            throw new RuntimeException("参数错误！待删除的数据主键id必须是大于0的整数");
        }
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @PutMapping("/pay/update")
    @Operation(summary = "修改", description = "修改支付信息，入参为json格式")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("修改成功，影响记录行数：" + i);
    }

    @GetMapping("/pay/get/{id}")
    @Operation(summary = "查询", description = "根据id查询支付信息")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        // 业务暂停62s
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        Pay pay = payService.queryById(id);
        return ResultData.success(pay);
    }

    @GetMapping("/pay/get/all")
    @Operation(summary = "查询", description = "查询所有支付信息")
    public ResultData<List<Pay>> getAll() {
        List<Pay> all = payService.getAll();
        return ResultData.success(all);
    }

    @Value("${server.port}")
    private String port;
    @GetMapping("/pay/get/info")
    public String getInfoByConsul(@Value("${xiaoxiecloud.info}") String info) {
        return "xiaoxiecloud:" + info + "\t" + "port:" + port;
    }
}
